package controller;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;

import model.Observer;
import model.Subject;
import stat.StatManager;
import stat.StatType;
import view.overlay.SkillPointAssignmentOverlay;

public class SkillPointAssignmentController extends Controller implements Observer {
	
	private SkillPointAssignmentOverlay mOverlay;
	
	private GameControl mToggleControl;
	private StatManager mStatManager;
	
	public SkillPointAssignmentController(SkillPointAssignmentOverlay spao, StatManager sm) {
		mOverlay = spao;
		mStatManager = sm;
		
		mStatManager.register(this);
		
		updateOverlay();
	}
	
	public void showAssigner() {
		mOverlay.showSkillPointAssigner();
	}
	
	public void hideAssigner() {
		mOverlay.hideSkillPointAssigner();
	}
	
	public void toggleAssigner() {
		mOverlay.toggleSkillPointAssigner();
	}
	
	public void deactivateAssigner() {
		mOverlay.setVisible(false);
	}
	
	public void activateAssigner() {
		mOverlay.setVisible(true);
	}
	
	private void updateOverlay() {
		StatType[] mUpgradeableStats = {
			    StatType.STR,
			    StatType.AGI,
			    StatType.INT,
			    StatType.HAR,
			    StatType.MVT,
			    StatType.VISIBILITY
		};
		
		mOverlay.setUpgradeableStats(mUpgradeableStats);
		
		EnumMap<StatType, MouseListener> listeners = new EnumMap<StatType, MouseListener>(StatType.class);
		EnumMap<StatType, Integer> values = new EnumMap<StatType, Integer>(StatType.class);
		
		for (final StatType type : mUpgradeableStats) {
			
			MouseListener up = new SimpleMouseListener() {

				@Override
				public void mouseClicked(MouseEvent e) {
					mStatManager.addPointToStat(type);
					
				}
				
			};
			
			listeners.put(type, up);
			
			
			values.put(type, mStatManager.getStatList().getStat(type));
			
			
		}
		
		values.put(StatType.STATPOINT, mStatManager.getStatList().getStat(StatType.STATPOINT));
		
		mOverlay.setUpListeners(listeners);
		mOverlay.setStatValues(values);
	}

	@Override
	public List<GameControl> getKeyControls() {
		List<GameControl> ctrls = new ArrayList<GameControl>();
		
		mToggleControl = new TimedGameControl(KeyEvent.VK_P, "TOGGLE_SKILL_POINT_ASSIGNMENT", "Show/Hide Skill Assignment", 500) {

			@Override
			protected void execute() {
				toggleAssigner();
				
			}
			
		};
		
		ctrls.add(mToggleControl);
		
		return (ctrls);
	}

	@Override
	public void update(Subject sourceSubject) {
		// update the UI
		updateOverlay();
	}

	@Override
	public void reset() {
		hideAssigner();
	}
}
